/**
 * carl版
 */

//写法1  
/**
 * 
 * @param {int} nums 
 * @param {int} target 
 * @returns int
 * 左右区间闭合[left,right]  所以 left==right  有意义
 * 下一次区间变化  left=mid+1   right=mid-1  
 */
function binarySearch(nums,target){
    let left=0,
        right=nums.length-1
    while(left<=right){
        let mid = Math.floor((right+left)/2)
        if(nums[mid]===target) return mid
        else if(nums[mid]>target) right=mid-1
        else left=mid+1
    }
    return -1
}



//写法二
/**
 * 区间 左闭右开 [left,right) 所以  while(left<right)
 * 下一次取值left=mid+1  right=mid  
 * @param {int} nums 
 * @param {int} target 
 */

function binarySearch(nums,target){
    let left=0,
        right=nums.length
    while(left<right){
        let mid = Math.floor((left+right)/2)
        if(nums[mid]===target) return mid
        else if(nums[mid]>target)  right=mid
        else left=mid+1
    }
    return -1
}

